package com.sdu.cloudservice.action;

import com.auth0.jwt.JWT;
import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.entity.AdminEntity;
import com.sdu.cloudservice.service.AdminService;
import com.sdu.cloudservice.token.AdminToken;
import com.sdu.cloudservice.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 6.25 klxlxf
 */
@Controller
@CrossOrigin
@RequestMapping("/admin")
public class AdminAction {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(String username,String password){
        try{
            Envelope envelope = adminService.login(username,password);
            if(envelope.getCode() != 0)
                return envelope.toString();
            String token = PasswordUtil.getToken((AdminEntity) envelope.getObj());
            envelope.setToken(token);
            return envelope.toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
//
//
//
//        Envelope envelope = Envelope.success;
//        if(username.equals("admin123456")&&password.equals("123456")){
//            AdminEntity adminEntity =  new AdminEntity();
//            adminEntity.setUsername("admin123456");
//            adminEntity.setPassword("123456");
//            String token = PasswordUtil.getToken(adminEntity);
//            envelope.setToken(token);
////            envelope.setObj("user");
//            return envelope.toString();
//        }
//        return new Envelope(1,"用户名或密码错误",null).toString();
    }
    @RequestMapping(value = "/userList",method = RequestMethod.GET)
    @ResponseBody
    @AdminToken
    public String userList(@RequestParam(name = "page",defaultValue = "1") int page, int code){
        //System.out.println(page);
        Envelope envelope = adminService.getUserList(page,code);
        if(envelope.getCode()!=0){
            return envelope.toString();
        }
        return envelope.toString();
    }

    @RequestMapping("/userListNum")
    @ResponseBody
    @AdminToken
    public String userListNum(){
        try{
            return adminService.getPageNumber().toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }

    /**
     * 权限
     * @return
     */
    @RequestMapping(value = "/role")
    @ResponseBody
    //@UserToken
    public String role(HttpServletRequest httpServletRequest){
//        session.getSessionContext().getSession()
        // session.setAttribute("user",null);
        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        if(token == null){
            return new Envelope(1024,"请先登录",null).toString();
        }
        // 从token还原用户名
        String role = JWT.decode(token).getClaim("identity").asString();
        return new Envelope("['"+ role + "']").toString();
    }
}
